package com.lee.servlet;

import com.alibaba.fastjson.JSON;
import com.lee.Interface.IMethod;
import com.lee.constant.CommonConstant;
import com.lee.dto.base.BaseResponse;
import com.lee.dto.base.RequestInfo;
import com.lee.impl.Method1;
import com.lee.impl.Method2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User:liwei
 * Date:2017/8/31
 * Time:14:29
 */
@WebServlet(urlPatterns = "/test",description = "test服务")
public class TestServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        //封装接口的参数
        RequestInfo requestInfo = RequestInfo.init(request);
        BaseResponse baseResponse = new BaseResponse();
        IMethod method = null;
        //接口派发
        if(requestInfo.getMethod().equals("test1")){
            method = new Method1();
        }else if(requestInfo.getMethod().equals("test2")){
            method = new Method2();
        }
        if(method == null){
            baseResponse.setSuccess(CommonConstant.ERROR);
        }else{
            baseResponse = method.doMethod(requestInfo);
        }
        response.getWriter().println(JSON.toJSONString(baseResponse));
    }
}
